package cn.itsource.pethome.org.controller;

import cn.itsource.pethome.basic.util.AjaxResult;
import cn.itsource.pethome.basic.util.PageInfo;
import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.org.domain.Shop;
import cn.itsource.pethome.org.query.ShopQuery;
import cn.itsource.pethome.org.service.IEmployeeService;
import cn.itsource.pethome.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(tags = "商店管理所有的接口",description = "商店接口描述")
public class ShopController {
    @Autowired
    private IShopService shopService;

    @PostMapping// 相当于@RequestMapping(method = RequestMethod.POST)
    @Transactional
    @ApiOperation(value = "添加或修改商店",notes = "有id为修改，无id为添加")
    public AjaxResult sava(@RequestBody Shop shop){
        try {
            shopService.save(shop);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后再试！！！");
        }
    }

    @DeleteMapping("/{id}")// 相当于@RequestMapping(method = RequestMethod.POST)
    @Transactional
    @ApiOperation(value = "根据ID删除商店")
    public AjaxResult delete(@PathVariable("id") Long id){//@PathVariable必须写
        try {
            shopService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后再试！！！");
        }
    }

    @GetMapping("/{id}")// 相当于@RequestMapping(method = RequestMethod.GET)
    @ApiOperation(value = "根据ID查询商店")
    public Shop findById(@PathVariable("id") Long id){
        return shopService.findById(id);
    }

    @PatchMapping// 相当于@RequestMapping(method = RequestMethod.PATCH)
    @ApiOperation(value = "查询所有的商店",notes = "我们这个接口没有分页哦")
    public PageInfo<Shop> findPage(@RequestBody ShopQuery shopQuery){
        return shopService.findPage(shopQuery);
    }

    @PatchMapping("/batchRemove")
    @Transactional
    @ApiOperation(value = "根据IDs删除商店")
    public AjaxResult batchRemove(@RequestBody List<Shop> shops){
        try {
            System.out.println(shops.toString());
            shopService.deleteMany(shops);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后再试！！！");
        }
    }

    @PostMapping("/settlement")
    @Transactional
    @ApiOperation(value = "商店入住添加数据")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

}
